package com.subfg.payment.controller.auth;

import com.alibaba.fastjson.JSONObject;
import com.subfg.common.constant.PayMethodEnum;
import com.subfg.common.entity.call.request.RequestPayModel;
import com.subfg.common.entity.call.request.refund.ApplyRefundModel;
import com.subfg.common.entity.call.result.ResultPayModel;
import com.subfg.common.entity.common.CommonParam;
import com.subfg.common.entity.common.CommonResult;
import com.subfg.common.entity.excpetion.BusinessExceptionModel;
import com.subfg.common.rabbitmq.constant.ExchangeApi;
import com.subfg.common.rabbitmq.constant.QueueContentModel;
import com.subfg.common.rabbitmq.produer.MQSender;
import com.subfg.common.util.auth.GenerateId;
import com.subfg.common.util.common.StringUtil;
import com.subfg.payment.rabiitmq.constant.RoutingKeyApi;
import com.subfg.payment.util.ali.AliPayServer;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("payment")
public class PaymentController {
    private Logger log = LoggerFactory.getLogger(PaymentController.class);

    @Autowired
    private AliPayServer aliPayServer;
    @Autowired
    private GenerateId generateId;
    @Autowired
    private MQSender mqSender;

    @ApiOperation(value = "生成支付信息")
    @RequestMapping(value = "createPayInfo", method = {RequestMethod.POST})
    @ResponseBody
    public CommonResult createPayInfo(@RequestBody CommonParam<RequestPayModel> data) throws BusinessExceptionModel {
        RequestPayModel payModel = data.getData();
        PayMethodEnum payMethod = PayMethodEnum.getSysPatMethod(payModel.getChannel());
        log.info("生成==>>>{} 支付信息", payMethod.getPayType());
        ResultPayModel resultPayInfoModel = null;
        payModel.setAttach(JSONObject.toJSONString(payModel));
        // 三方支付 渠道判断
        switch (payMethod)  {
            case PAY_ALI:
                // 支付宝
                resultPayInfoModel = aliPayServer.createAlipayInfo(payModel);
                break;
            case PAY_WECHAT:
                // 微信
                break;
            case PAY_STRIPE:
                // Stripe
                break;
            default:
                log.warn("未找到合适生成的支付方式");
                break;
        }
        if (resultPayInfoModel == null || StringUtil.isEmpty(resultPayInfoModel.getOrderStr())) {
            log.error("未能生成正确的支付方式!请检查");
            throw new BusinessExceptionModel();
        }
        resultPayInfoModel.setPaymentRate(payModel.getPaymentRate());
        return CommonResult.ok(resultPayInfoModel);
    }

    @ApiOperation(value = "多订单退款")
    @RequestMapping(value = "moreRefund", method = {RequestMethod.POST})
    @ResponseBody
    public CommonResult moreRefund(@RequestBody CommonParam<ApplyRefundModel> data) throws BusinessExceptionModel {
        ApplyRefundModel applyRefundModel = data.getData();
        if (applyRefundModel.getRefundList().isEmpty()) {
            log.warn("退款信息为空");
            throw new BusinessExceptionModel();
        }
        mqSender.sendMQMessage(ExchangeApi.EXCHANGE_COMMON, RoutingKeyApi.PAYMENT_REFUND_INFO,
                new QueueContentModel(generateId.genrateMqId(), 3, 0, applyRefundModel));
        return CommonResult.ok();
    }


    @ApiOperation(value = "查询订单是否支付")
    @RequestMapping(value = "checkoutOrderBilling", method = {RequestMethod.POST})
    @ResponseBody
    public CommonResult checkoutOrderBilling() {

        return CommonResult.ok();
    }

    @ApiOperation(value = "提现")
    @RequestMapping(value = "withdraw", method = {RequestMethod.POST})
    @ResponseBody
    public CommonResult withdraw() {

        return CommonResult.ok();
    }



}
